// pages/shopManager/virtualOrder/orderList/orderList.js
const common = require('../../../../utils/util.js')
const app = getApp()
var QQMapWX = require('../../../../utils/qqmap-wx-jssdk.min.js')
var qqmapsdk
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: null,
        searchKey: '',
        orderType: 0,
        orderList: [],
        nowPage: 0,
        isLoading: false,
        isAll: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        qqmapsdk = new QQMapWX({
            key: 'NZMBZ-R4OEF-E7WJS-NRKOX-FWNFS-HHFLU'
        })
        this.getUserInfo()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.setData({
            searchKey: '',
            orderList: [],
            nowPage: 0,
            isLoading: false,
            isAll: false
        })
        this.getOrderList()
        setTimeout(function () {
            wx.stopPullDownRefresh()
        }, 2000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.getOrderList()
    },

    getUserInfo: function () {
        const that = this
        let data = {}
        common.post('User/user_info', data).then(function (res) {
            if (res.code == 204) {
                return
            } else {
                that.setData({
                    userInfo: res
                })
                that.getOrderList()
            }
        }).catch(function (e) { })
    },

    getOrderList: function () {
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        const that = this
        let data
        let orderList = this.data.orderList
        if (this.data.searchKey == '') {
            if (app.globalData.identity == 'manager') {
                if (this.data.orderType == 0) {
                    data = {
                        get_user: 1,
                        virtual_goods: 1,
                        paramter: 1,
                        limit: 10,
                        page: this.data.nowPage
                    }
                } else if (this.data.orderType == 1) {
                    data = {
                        get_user: 1,
                        virtual_goods: 1,
                        paramter: 1,
                        order_status: 0,
                        shipping_status: 0,
                        pay_status: 0,
                        limit: 10,
                        page: this.data.nowPage
                    }
                } else if (this.data.orderType == 2) {
                    data = {
                        get_user: 1,
                        virtual_goods: 1,
                        paramter: 1,
                        order_status: 0,
                        shipping_status: 0,
                        pay_status: 2,
                        limit: 10,
                        page: this.data.nowPage
                    }
                } else if (this.data.orderType == 3) {
                    data = {
                        get_user: 1,
                        virtual_goods: 1,
                        paramter: 1,
                        order_status: 1,
                        shipping_status: 2,
                        pay_status: 2,
                        limit: 10,
                        page: this.data.nowPage
                    }
                }
            } else if (app.globalData.identity == 'filiale') {
                if (this.data.orderType == 0) {
                    data = {
                        get_user: 1,
                        filiale_id: this.data.userInfo.filiale_id,
                        virtual_goods: 1,
                        paramter: 1,
                        limit: 10,
                        page: this.data.nowPage
                    }
                } else if (this.data.orderType == 1) {
                    data = {
                        get_user: 1,
                        filiale_id: this.data.userInfo.filiale_id,
                        virtual_goods: 1,
                        paramter: 1,
                        order_status: 0,
                        shipping_status: 0,
                        pay_status: 0,
                        limit: 10,
                        page: this.data.nowPage
                    }
                } else if (this.data.orderType == 2) {
                    data = {
                        get_user: 1,
                        filiale_id: this.data.userInfo.filiale_id,
                        virtual_goods: 1,
                        paramter: 1,
                        order_status: 0,
                        shipping_status: 0,
                        pay_status: 2,
                        limit: 10,
                        page: this.data.nowPage
                    }
                } else if (this.data.orderType == 3) {
                    data = {
                        get_user: 1,
                        filiale_id: this.data.userInfo.filiale_id,
                        virtual_goods: 1,
                        paramter: 1,
                        order_status: 1,
                        shipping_status: 2,
                        pay_status: 2,
                        limit: 10,
                        page: this.data.nowPage
                    }
                }
            } else if (app.globalData.identity == 'shop') {
                if (this.data.orderType == 0) {
                    data = {
                        get_user: 1,
                        store_id: this.data.userInfo.store_id,
                        virtual_goods: 1,
                        paramter: 1,
                        limit: 10,
                        page: this.data.nowPage
                    }
                } else if (this.data.orderType == 1) {
                    data = {
                        get_user: 1,
                        store_id: this.data.userInfo.store_id,
                        virtual_goods: 1,
                        paramter: 1,
                        order_status: 0,
                        shipping_status: 0,
                        pay_status: 0,
                        limit: 10,
                        page: this.data.nowPage
                    }
                } else if (this.data.orderType == 2) {
                    data = {
                        get_user: 1,
                        store_id: this.data.userInfo.store_id,
                        virtual_goods: 1,
                        paramter: 1,
                        order_status: 0,
                        shipping_status: 0,
                        pay_status: 2,
                        limit: 10,
                        page: this.data.nowPage
                    }
                } else if (this.data.orderType == 3) {
                    data = {
                        get_user: 1,
                        store_id: this.data.userInfo.store_id,
                        virtual_goods: 1,
                        paramter: 1,
                        order_status: 1,
                        shipping_status: 2,
                        pay_status: 2,
                        limit: 10,
                        page: this.data.nowPage
                    }
                }
            }
        } else {
            if (app.globalData.identity == 'manager') {
                if (this.data.orderType == 0) {
                    data = {
                        get_user: 1,
                        user_id: this.data.searchKey,
                        virtual_goods: 1,
                        paramter: 1,
                        limit: 10,
                        page: this.data.nowPage
                    }
                } else if (this.data.orderType == 1) {
                    data = {
                        get_user: 1,
                        user_id: this.data.searchKey,
                        virtual_goods: 1,
                        paramter: 1,
                        order_status: 0,
                        shipping_status: 0,
                        pay_status: 0,
                        limit: 10,
                        page: this.data.nowPage
                    }
                } else if (this.data.orderType == 2) {
                    data = {
                        get_user: 1,
                        user_id: this.data.searchKey,
                        virtual_goods: 1,
                        paramter: 1,
                        order_status: 0,
                        shipping_status: 0,
                        pay_status: 2,
                        limit: 10,
                        page: this.data.nowPage
                    }
                } else if (this.data.orderType == 3) {
                    data = {
                        get_user: 1,
                        user_id: this.data.searchKey,
                        virtual_goods: 1,
                        paramter: 1,
                        order_status: 1,
                        shipping_status: 2,
                        pay_status: 2,
                        limit: 10,
                        page: this.data.nowPage
                    }
                }
            } else if (app.globalData.identity == 'filiale') {
                if (this.data.orderType == 0) {
                    data = {
                        get_user: 1,
                        filiale_id: this.data.userInfo.filiale_id,
                        user_id: this.data.searchKey,
                        virtual_goods: 1,
                        paramter: 1,
                        limit: 10,
                        page: this.data.nowPage
                    }
                } else if (this.data.orderType == 1) {
                    data = {
                        get_user: 1,
                        filiale_id: this.data.userInfo.filiale_id,
                        user_id: this.data.searchKey,
                        virtual_goods: 1,
                        paramter: 1,
                        order_status: 0,
                        shipping_status: 0,
                        pay_status: 0,
                        limit: 10,
                        page: this.data.nowPage
                    }
                } else if (this.data.orderType == 2) {
                    data = {
                        get_user: 1,
                        filiale_id: this.data.userInfo.filiale_id,
                        user_id: this.data.searchKey,
                        virtual_goods: 1,
                        paramter: 1,
                        order_status: 0,
                        shipping_status: 0,
                        pay_status: 2,
                        limit: 10,
                        page: this.data.nowPage
                    }
                } else if (this.data.orderType == 3) {
                    data = {
                        get_user: 1,
                        filiale_id: this.data.userInfo.filiale_id,
                        user_id: this.data.searchKey,
                        virtual_goods: 1,
                        paramter: 1,
                        order_status: 1,
                        shipping_status: 2,
                        pay_status: 2,
                        limit: 10,
                        page: this.data.nowPage
                    }
                }
            } else if (app.globalData.identity == 'shop') {
                if (this.data.orderType == 0) {
                    data = {
                        get_user: 1,
                        store_id: this.data.userInfo.store_id,
                        user_id: this.data.searchKey,
                        virtual_goods: 1,
                        paramter: 1,
                        limit: 10,
                        page: this.data.nowPage
                    }
                } else if (this.data.orderType == 1) {
                    data = {
                        get_user: 1,
                        store_id: this.data.userInfo.store_id,
                        user_id: this.data.searchKey,
                        virtual_goods: 1,
                        paramter: 1,
                        order_status: 0,
                        shipping_status: 0,
                        pay_status: 0,
                        limit: 10,
                        page: this.data.nowPage
                    }
                } else if (this.data.orderType == 2) {
                    data = {
                        get_user: 1,
                        store_id: this.data.userInfo.store_id,
                        user_id: this.data.searchKey,
                        virtual_goods: 1,
                        paramter: 1,
                        order_status: 0,
                        shipping_status: 0,
                        pay_status: 2,
                        limit: 10,
                        page: this.data.nowPage
                    }
                } else if (this.data.orderType == 3) {
                    data = {
                        get_user: 1,
                        store_id: this.data.userInfo.store_id,
                        user_id: this.data.searchKey,
                        virtual_goods: 1,
                        paramter: 1,
                        order_status: 1,
                        shipping_status: 2,
                        pay_status: 2,
                        limit: 10,
                        page: this.data.nowPage
                    }
                }
            }
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '加载中',
        })
        common.post('Orderlist/user_order', data).then(function (res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    orderList: orderList,
                    isAll: true
                })
            } else {
                orderList = orderList.concat(res)
                that.setData({
                    orderList: orderList,
                    nowPage: that.data.nowPage + 1
                })
            }
        }).catch(function (e) { })
    },

    navClick: function (e) {
        if (e.currentTarget.dataset.type == this.data.orderType) {
            return
        }
        this.setData({
            orderType: e.currentTarget.dataset.type,
            orderList: [],
            nowPage: 0,
            isLoading: false,
            isAll: false
        })
        this.getOrderList()
    },

    goOrderDetail: function (e) {
        wx.navigateTo({
            url: '/pages/shopManager/virtualOrder/orderDetail/orderDetail?orderId=' + e.currentTarget.dataset.id
        })
    },

    cancleOrder: function (e) {
        const that = this
        wx.showModal({
            title: '提示',
            content: '确认取消订单？',
            success(res) {
                if (res.confirm) {
                    wx.showLoading({
                        title: '请稍后'
                    })
                    let data = {
                        order_id: e.currentTarget.dataset.id
                    }
                    common.post('Orderlist/cancel_order', data).then(function (res) {
                        wx.hideLoading()
                        if (res.code == 204) {
                            wx.showModal({
                                title: '提示',
                                content: res.error,
                                showCancel: false,
                                success(res) { }
                            })
                            return
                        }
                        wx.showModal({
                            title: '提示',
                            content: '成功取消订单',
                            showCancel: false,
                            success(res) {
                                if (res.confirm) {
                                    that.setData({
                                        orderList: [],
                                        nowPage: 0,
                                        isLoading: false,
                                        isAll: false
                                    })
                                    that.getOrderList()
                                }
                            }
                        })
                    }).catch(function (e) { })
                }
            }
        })
    },

    goPay: function (e) {
        const that = this
        wx.login({
            success(respone) {
                if (respone.code) {
                    //发起网络请求
                    wx.showLoading({
                        title: '支付中'
                    })
                    let data = {
                        order_sn: e.currentTarget.dataset.sn,
                        code: respone.code,
                        pay_client: 1
                    }
                    common.post('Orderlist/sm_again_pay_order', data).then(function (res) {
                        wx.hideLoading()
                        if (res.code == 204) {
                            wx.showModal({
                                title: '提示',
                                content: res.error,
                                showCancel: false,
                                success(res) { }
                            })
                            return
                        }
                        wx.requestPayment({
                            timeStamp: res.timeStamp,
                            nonceStr: res.nonceStr,
                            package: res.package,
                            signType: res.signType,
                            paySign: res.paySign,
                            success(res) {
                                that.setData({
                                    orderType: 2,
                                    orderList: [],
                                    nowPage: 0,
                                    isLoading: false,
                                    isAll: false
                                })
                                that.getOrderList()
                            },
                            fail(res) { },
                        })
                    }).catch(function (e) { })
                }
            }
        })
    },

    goNavigation: function (e) {
        const that = this
        qqmapsdk.geocoder({
            //获取表单传入地址
            address: e.currentTarget.dataset.address, //地址参数，例：固定地址，address: '北京市海淀区彩和坊路海淀西大街74号'
            success: function (res) { //成功后的回调
                const latitude = res.result.location.lat
                const longitude = res.result.location.lng
                wx.openLocation({
                    latitude,
                    longitude,
                    name: e.currentTarget.dataset.address
                })
            }
        })
    },

    searchInput: function (e) {
        this.setData({
            searchKey: e.detail.value
        })
    },

    goSearch: function () {
        this.setData({
            orderList: [],
            nowPage: 0,
            isLoading: false,
            isAll: false
        })
        this.getOrderList()
    },

    updateData: function () {
        const that = this
        let data
        if (this.data.searchKey == '') {
            if (app.globalData.identity == 'manager') {
                if (this.data.orderType == 0) {
                    data = {
                        get_user: 1,
                        virtual_goods: 1,
                        paramter: 1,
                        limit: 10 * this.data.nowPage,
                        page: 0
                    }
                } else if (this.data.orderType == 1) {
                    data = {
                        get_user: 1,
                        virtual_goods: 1,
                        paramter: 1,
                        order_status: 0,
                        shipping_status: 0,
                        pay_status: 0,
                        limit: 10 * this.data.nowPage,
                        page: 0
                    }
                } else if (this.data.orderType == 2) {
                    data = {
                        get_user: 1,
                        virtual_goods: 1,
                        paramter: 1,
                        order_status: 0,
                        shipping_status: 0,
                        pay_status: 2,
                        limit: 10 * this.data.nowPage,
                        page: 0
                    }
                } else if (this.data.orderType == 3) {
                    data = {
                        get_user: 1,
                        virtual_goods: 1,
                        paramter: 1,
                        order_status: 1,
                        shipping_status: 2,
                        pay_status: 2,
                        limit: 10 * this.data.nowPage,
                        page: 0
                    }
                }
            } else if (app.globalData.identity == 'filiale') {
                if (this.data.orderType == 0) {
                    data = {
                        get_user: 1,
                        filiale_id: this.data.userInfo.filiale_id,
                        virtual_goods: 1,
                        paramter: 1,
                        limit: 10 * this.data.nowPage,
                        page: 0
                    }
                } else if (this.data.orderType == 1) {
                    data = {
                        get_user: 1,
                        filiale_id: this.data.userInfo.filiale_id,
                        virtual_goods: 1,
                        paramter: 1,
                        order_status: 0,
                        shipping_status: 0,
                        pay_status: 0,
                        limit: 10 * this.data.nowPage,
                        page: 0
                    }
                } else if (this.data.orderType == 2) {
                    data = {
                        get_user: 1,
                        filiale_id: this.data.userInfo.filiale_id,
                        virtual_goods: 1,
                        paramter: 1,
                        order_status: 0,
                        shipping_status: 0,
                        pay_status: 2,
                        limit: 10 * this.data.nowPage,
                        page: 0
                    }
                } else if (this.data.orderType == 3) {
                    data = {
                        get_user: 1,
                        filiale_id: this.data.userInfo.filiale_id,
                        virtual_goods: 1,
                        paramter: 1,
                        order_status: 1,
                        shipping_status: 2,
                        pay_status: 2,
                        limit: 10 * this.data.nowPage,
                        page: 0
                    }
                }
            } else if (app.globalData.identity == 'shop') {
                if (this.data.orderType == 0) {
                    data = {
                        get_user: 1,
                        store_id: this.data.userInfo.store_id,
                        virtual_goods: 1,
                        paramter: 1,
                        limit: 10 * this.data.nowPage,
                        page: 0
                    }
                } else if (this.data.orderType == 1) {
                    data = {
                        get_user: 1,
                        store_id: this.data.userInfo.store_id,
                        virtual_goods: 1,
                        paramter: 1,
                        order_status: 0,
                        shipping_status: 0,
                        pay_status: 0,
                        limit: 10 * this.data.nowPage,
                        page: 0
                    }
                } else if (this.data.orderType == 2) {
                    data = {
                        get_user: 1,
                        store_id: this.data.userInfo.store_id,
                        virtual_goods: 1,
                        paramter: 1,
                        order_status: 0,
                        shipping_status: 0,
                        pay_status: 2,
                        limit: 10 * this.data.nowPage,
                        page: 0
                    }
                } else if (this.data.orderType == 3) {
                    data = {
                        get_user: 1,
                        store_id: this.data.userInfo.store_id,
                        virtual_goods: 1,
                        paramter: 1,
                        order_status: 1,
                        shipping_status: 2,
                        pay_status: 2,
                        limit: 10 * this.data.nowPage,
                        page: 0
                    }
                }
            }
        } else {
            if (app.globalData.identity == 'manager') {
                if (this.data.orderType == 0) {
                    data = {
                        get_user: 1,
                        user_id: this.data.searchKey,
                        virtual_goods: 1,
                        paramter: 1,
                        limit: 10 * this.data.nowPage,
                        page: 0
                    }
                } else if (this.data.orderType == 1) {
                    data = {
                        get_user: 1,
                        user_id: this.data.searchKey,
                        virtual_goods: 1,
                        paramter: 1,
                        order_status: 0,
                        shipping_status: 0,
                        pay_status: 0,
                        limit: 10 * this.data.nowPage,
                        page: 0
                    }
                } else if (this.data.orderType == 2) {
                    data = {
                        get_user: 1,
                        user_id: this.data.searchKey,
                        virtual_goods: 1,
                        paramter: 1,
                        order_status: 0,
                        shipping_status: 0,
                        pay_status: 2,
                        limit: 10 * this.data.nowPage,
                        page: 0
                    }
                } else if (this.data.orderType == 3) {
                    data = {
                        get_user: 1,
                        user_id: this.data.searchKey,
                        virtual_goods: 1,
                        paramter: 1,
                        order_status: 1,
                        shipping_status: 2,
                        pay_status: 2,
                        limit: 10 * this.data.nowPage,
                        page: 0
                    }
                }
            } else if (app.globalData.identity == 'filiale') {
                if (this.data.orderType == 0) {
                    data = {
                        get_user: 1,
                        filiale_id: this.data.userInfo.filiale_id,
                        user_id: this.data.searchKey,
                        virtual_goods: 1,
                        paramter: 1,
                        limit: 10 * this.data.nowPage,
                        page: 0
                    }
                } else if (this.data.orderType == 1) {
                    data = {
                        get_user: 1,
                        filiale_id: this.data.userInfo.filiale_id,
                        user_id: this.data.searchKey,
                        virtual_goods: 1,
                        paramter: 1,
                        order_status: 0,
                        shipping_status: 0,
                        pay_status: 0,
                        limit: 10 * this.data.nowPage,
                        page: 0
                    }
                } else if (this.data.orderType == 2) {
                    data = {
                        get_user: 1,
                        filiale_id: this.data.userInfo.filiale_id,
                        user_id: this.data.searchKey,
                        virtual_goods: 1,
                        paramter: 1,
                        order_status: 0,
                        shipping_status: 0,
                        pay_status: 2,
                        limit: 10 * this.data.nowPage,
                        page: 0
                    }
                } else if (this.data.orderType == 3) {
                    data = {
                        get_user: 1,
                        filiale_id: this.data.userInfo.filiale_id,
                        user_id: this.data.searchKey,
                        virtual_goods: 1,
                        paramter: 1,
                        order_status: 1,
                        shipping_status: 2,
                        pay_status: 2,
                        limit: 10 * this.data.nowPage,
                        page: 0
                    }
                }
            } else if (app.globalData.identity == 'shop') {
                if (this.data.orderType == 0) {
                    data = {
                        get_user: 1,
                        store_id: this.data.userInfo.store_id,
                        user_id: this.data.searchKey,
                        virtual_goods: 1,
                        paramter: 1,
                        limit: 10 * this.data.nowPage,
                        page: 0
                    }
                } else if (this.data.orderType == 1) {
                    data = {
                        get_user: 1,
                        store_id: this.data.userInfo.store_id,
                        user_id: this.data.searchKey,
                        virtual_goods: 1,
                        paramter: 1,
                        order_status: 0,
                        shipping_status: 0,
                        pay_status: 0,
                        limit: 10 * this.data.nowPage,
                        page: 0
                    }
                } else if (this.data.orderType == 2) {
                    data = {
                        get_user: 1,
                        store_id: this.data.userInfo.store_id,
                        user_id: this.data.searchKey,
                        virtual_goods: 1,
                        paramter: 1,
                        order_status: 0,
                        shipping_status: 0,
                        pay_status: 2,
                        limit: 10 * this.data.nowPage,
                        page: 0
                    }
                } else if (this.data.orderType == 3) {
                    data = {
                        get_user: 1,
                        store_id: this.data.userInfo.store_id,
                        user_id: this.data.searchKey,
                        virtual_goods: 1,
                        paramter: 1,
                        order_status: 1,
                        shipping_status: 2,
                        pay_status: 2,
                        limit: 10 * this.data.nowPage,
                        page: 0
                    }
                }
            }
        }
        wx.showLoading({
            title: '加载中',
        })
        common.post('Orderlist/user_order', data).then(function (res) {
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    orderList: []
                })
            } else {
                that.setData({
                    orderList: res
                })
            }
        }).catch(function (e) { })
    }
})